var admin_medical_object = {};
admin_medical_object.act_filter = function(frm_id) {
    if (frm_id == null || frm_id == undefined) {
        $("#frm_object_search").submit();
    } else {
        $("#" + frm_id).submit();
    }
    return false;
};
admin_medical_object.reset_filters = function(frm_id) {
    $("#" + frm_id).find("textarea, select, input:text").val(null);
    admin_medical_object.act_filter();
    return false;
};

admin_medical_object.go_page = function(obj) {
    var page = $(obj).text();
    if (page == null || page == undefined) {
        page = 1;
    }
    $("#current_page").val(page);
    return admin_medical_object.act_filter();
};

admin_medical_object.first_page = function() {
    $("#current_page").val(1);
    return admin_medical_object.act_filter();
};

admin_medical_object.prev_page = function() {
    var page = parseInt($("#current_page").val());
    page--;
    $("#current_page").val(page);
    return admin_medical_object.act_filter();
};

admin_medical_object.next_page = function(last_page) {
    var page = parseInt($("#current_page").val());
    page++;
    if (page > last_page) {
        return false;
    }
    $("#current_page").val(page);
    return admin_medical_object.act_filter();
};

admin_medical_object.last_page = function(page) {
    $("#current_page").val(page);
    return admin_medical_object.act_filter();
};

admin_medical_object.submit_object = function() {
    var name     = $(".obj_name").val().trim();
    var age_from = $(".obj_age_from").val().trim();
    var age_to   = $(".obj_age_to").val().trim();

    if(name == ''){
        jQuery("#form_error").html('Tên đối tượng không dược để trống');
        jQuery("#form_error").addClass('error_message');
        jQuery('.obj_name').focus();
        return false;
    }
    if(age_from == '' || age_to == ''){
        jQuery("#form_error").html('Độ tuổi không dược để trống');
        jQuery("#form_error").addClass('error_message');
        return false;
    }

    jQuery("#frm_object_edit").submit();
    return;
};

/* -------------------------------------------js parts --------------------------------*/
var admin_medical_parts = {};
admin_medical_parts.act_filter = function(frm_id) {
    if (frm_id == null || frm_id == undefined) {
        $("#frm_parts_search").submit();
    } else {
        $("#" + frm_id).submit();
    }
    return false;
};

admin_medical_parts.reset_filters = function(frm_id) {
    $("#" + frm_id).find("textarea, select, input:text").val(null);
    admin_medical_parts.act_filter();
    return false;
};

admin_medical_parts.go_page = function(obj) {
    var page = $(obj).text();
    if (page == null || page == undefined) {
        page = 1;
    }
    $("#current_page").val(page);
    return admin_medical_parts.act_filter();
};

admin_medical_parts.first_page = function() {
    $("#current_page").val(1);
    return admin_medical_parts.act_filter();
};

admin_medical_parts.prev_page = function() {
    var page = parseInt($("#current_page").val());
    page--;
    $("#current_page").val(page);
    return admin_medical_parts.act_filter();
};

admin_medical_parts.next_page = function(last_page) {
    var page = parseInt($("#current_page").val());
    page++;
    if (page > last_page) {
        return false;
    }
    $("#current_page").val(page);
    return admin_medical_parts.act_filter();
};

admin_medical_parts.last_page = function(page) {
    $("#current_page").val(page);
    return admin_medical_parts.act_filter();
};

admin_medical_parts.submit_parts = function() {
    var name     = $(".obj_name").val().trim();
    var age_from = $(".obj_age_from").val().trim();
    var age_to   = $(".obj_age_to").val().trim();

    if(name == ''){
        jQuery("#form_error").html('Tên đối tượng không dược để trống');
        jQuery("#form_error").addClass('error_message');
        jQuery('.obj_name').focus();
        return false;
    }
    if(age_from == '' || age_to == ''){
        jQuery("#form_error").html('Độ tuổi không dược để trống');
        jQuery("#form_error").addClass('error_message');
        return false;
    }

    jQuery("#frm_object_edit").submit();
    return;
};

admin_medical_parts.show_img_path = function(checked) {
    if (checked) {
        $("#wp_img_path").show();
        $("#wp_img_path input").focus();
    } else {
        $("#wp_img_path").hide();
    }
};

//Hiện tất cả checkbox chọn khoa
admin_medical_parts.showChkDep = function(allDep, element_id, chkId){
    var html = '';

    for(var i in allDep){
        var is_check = false;
        html += '<span style="float: left;">';

        if(chkId != undefined && chkId){ //kiểm tra checkbox này có dc check ko
            if(chkId[allDep[i]['id']] != undefined){
                is_check = true;
            }
        }

        if(is_check){
            html += '<input type="checkbox" class="required" title="Bạn phải chọn khoa" name="data[department_ids][]" id="dep_'+ allDep[i]['id'] +'" value="'+ allDep[i]['id'] +'" checked="checked" />';
        }else{
            html += '<input type="checkbox" class="required" title="Bạn phải chọn khoa" name="data[department_ids][]" id="dep_'+ allDep[i]['id'] +'" value="'+ allDep[i]['id'] +'" />';
        }

        html += '<label style="font-weight: bold;margin-left: 5px;margin-right: 15px;position: relative;top: -2px;cursor: pointer;" for="dep_'+ allDep[i]['id'] +'">'+ allDep[i]['name'] +'</label>';
        html += '</span>';
    }
    jQuery("#"+element_id).html(html);
};



//Chọn bộ phận cơ thể cha dùng đệ quy
admin_medical_parts.showOptParts = function(allParts, element_id, chkId){
    while (jQuery("#"+element_id).attr('length')> 0) {
        jQuery("#"+element_id).children("option").remove();
    }
    var obj   = window.document.getElementById(element_id);
    var first = true,option = document.createElement('option');

    option.value = 0;
    option.text = "Chọn bộ phận";
    option.selected = true;
    obj.options.add(option);

    for(var i in allParts){
        if(allParts[i]['parent_id'] == 0){
            var cat_option   = document.createElement('option');
            cat_option.value = allParts[i]['id'];
            cat_option.text  = allParts[i]['name'];
            if(chkId != undefined && parseInt(chkId) == allParts[i]['id']){
                cat_option.selected = 'selected';
            }
            obj.options.add(cat_option);

            var parent_id = allParts[i]['id'];
            admin_medical_parts.showChildOptParts(allParts, obj, parent_id, chkId, 1);

        }
    }
};

admin_medical_parts.showChildOptParts = function(allParts, obj, parent_id, chkId, level){
    var pre_text = '';

    for(var i = 0; i < level; i++){
        pre_text += '---'
    }
    level++;

    for(var i in allParts){
        if(allParts[i]['parent_id'] == parent_id){
            var cat_option   = document.createElement('option');
            cat_option.value = allParts[i]['id'];
            cat_option.text  = pre_text + allParts[i]['name'];
            if(chkId != undefined && parseInt(chkId) == allParts[i]['id']){
                cat_option.selected = 'selected';
            }
            obj.options.add(cat_option);

            admin_medical_parts.showChildOptParts(allParts, obj, allParts[i]['id'], chkId, level);
        }
    }
};


/* -------------------------------------------js department --------------------------------*/
var admin_medical_department = {};
admin_medical_department.act_filter = function(frm_id) {
    if (frm_id == null || frm_id == undefined) {
        $("#frm_department_search").submit();
    } else {
        $("#" + frm_id).submit();
    }
    return false;
};

admin_medical_department.reset_filters = function(frm_id) {
    $("#" + frm_id).find("textarea, select, input:text").val(null);
    admin_medical_department.act_filter();
    return false;
};

admin_medical_department.go_page = function(obj) {
    var page = $(obj).text();
    if (page == null || page == undefined) {
        page = 1;
    }
    $("#current_page").val(page);
    return admin_medical_department.act_filter();
};

admin_medical_department.first_page = function() {
    $("#current_page").val(1);
    return admin_medical_department.act_filter();
};

admin_medical_department.prev_page = function() {
    var page = parseInt($("#current_page").val());
    page--;
    $("#current_page").val(page);
    return admin_medical_department.act_filter();
};

admin_medical_department.next_page = function(last_page) {
    var page = parseInt($("#current_page").val());
    page++;
    if (page > last_page) {
        return false;
    }
    $("#current_page").val(page);
    return admin_medical_department.act_filter();
};

admin_medical_department.last_page = function(page) {
    $("#current_page").val(page);
    return admin_medical_department.act_filter();
};


/* -------------------------------------------js symptoms --------------------------------*/
var admin_medical_symptoms = {};
admin_medical_symptoms.act_filter = function(frm_id) {
    if (frm_id == null || frm_id == undefined) {
        $("#frm_symptoms_search").submit();
    } else {
        $("#" + frm_id).submit();
    }
    return false;
};

admin_medical_symptoms.reset_filters = function(frm_id) {
    $("#" + frm_id).find("textarea, select, input:text").val(null);
    admin_medical_symptoms.act_filter();
    return false;
};

admin_medical_symptoms.go_page = function(obj) {
    var page = $(obj).text();
    if (page == null || page == undefined) {
        page = 1;
    }
    $("#current_page").val(page);
    return admin_medical_symptoms.act_filter();
};

admin_medical_symptoms.first_page = function() {
    $("#current_page").val(1);
    return admin_medical_symptoms.act_filter();
};

admin_medical_symptoms.prev_page = function() {
    var page = parseInt($("#current_page").val());
    page--;
    $("#current_page").val(page);
    return admin_medical_symptoms.act_filter();
};

admin_medical_symptoms.next_page = function(last_page) {
    var page = parseInt($("#current_page").val());
    page++;
    if (page > last_page) {
        return false;
    }
    $("#current_page").val(page);
    return admin_medical_symptoms.act_filter();
};

admin_medical_symptoms.last_page = function(page) {
    $("#current_page").val(page);
    return admin_medical_symptoms.act_filter();
};

/* -------------------------------------------js disease --------------------------------*/
var admin_medical_disease = {};
admin_medical_disease.act_filter = function(frm_id) {
    if (frm_id == null || frm_id == undefined) {
        $("#frm_disease_search").submit();
    } else {
        $("#" + frm_id).submit();
    }
    return false;
};

admin_medical_disease.reset_filters = function(frm_id) {
    $("#" + frm_id).find("textarea, select, input:text").val(null);
    admin_medical_disease.act_filter();
    return false;
};

admin_medical_disease.go_page = function(obj) {
    var page = $(obj).text();
    if (page == null || page == undefined) {
        page = 1;
    }
    $("#current_page").val(page);
    return admin_medical_disease.act_filter();
};

admin_medical_disease.first_page = function() {
    $("#current_page").val(1);
    return admin_medical_disease.act_filter();
};

admin_medical_disease.prev_page = function() {
    var page = parseInt($("#current_page").val());
    page--;
    $("#current_page").val(page);
    return admin_medical_disease.act_filter();
};

admin_medical_disease.next_page = function(last_page) {
    var page = parseInt($("#current_page").val());
    page++;
    if (page > last_page) {
        return false;
    }
    $("#current_page").val(page);
    return admin_medical_disease.act_filter();
};

admin_medical_disease.last_page = function(page) {
    $("#current_page").val(page);
    return admin_medical_disease.act_filter();
};

admin_medical_disease.filter_disease_related = function(result_id, type) {//mặc định là bệnh liên quan còn type = 1 triệu chứng liên quan
    if(type == undefined){
        type = 0;
    }

    var list_id = "disease_related";

    var data = $("form").serialize();
    data += "&ajax_action=true&type=" + type;
    $.ajax({
        type: "POST",
        url: BASE_URL + "admin/medical/disease/disease_relate",
        data: data
    }).done(function(output) {
            $("#" + result_id).html(output);

            var list = $("#" + list_id);
            if ($(list).size() == 0) {
                return false;
            }
            $(list).find("li").each(function() {
                var id = $(this).attr("id").toString().replace("related_id_", "");
                var btn_related = $("#btn_related_" + id);
                if ($(btn_related).size() > 0) {
                    $(btn_related).hide();
                }
            });
        });
    return false;
};

admin_medical_disease.reset_filter_related = function(result_id, type) { //mặc định là bệnh liên quan còn type = 1 triệu chứng liên quan
    var list_id = "disease_related";
    var data = "ajax_action=true";
    $.ajax({
        type: "POST",
        url: BASE_URL + "admin/medical/disease/disease_relate",
        data: data
    }).done(function(output) {
            $("#" + result_id).html(output);

            var list = $("#" + list_id);
            if ($(list).size() == 0) {
                return false;
            }
            $(list).find("li").each(function() {
                var id = $(this).attr("id").toString().replace("li_disease_relate", "");
                var btn_related = $("#btn_disease_relate" + id);
                if ($(btn_related).size() > 0) {
                    $(btn_related).hide();
                }
            });
        });
    return false;
};

admin_medical_disease.add_disease_related = function(id, title,edit_url, list_id) {
    if (id == null || id == undefined) {
        return false;
    }
    if (title == null || title == undefined) {
        return false;
    }

    $("#" + list_id).show();

    var list = $("#" + list_id);

    if ($(list).size() == 0) {
        return false;
    }
    var selected_item = $("#li_disease_relate" + id);
    if ($(selected_item).size() > 0) {
        alert('Bệnh lý này đã được chọn');
        return false;
    }
    var btn_related = $("#btn_disease_relate" + id);
    if ($(btn_related).size() > 0) {
        $(btn_related).hide();
    }
    var item = $("<li id='li_disease_relate"+id+"'><span title='Xóa khỏi danh sách liên quan' class='icon icon_delete' onclick='admin_medical_disease.remove_related("+id+")'></span><a target='_blank' href='"+edit_url+"'>"+title+"</a><input type='hidden' name='data[disease_ids][]' value='"+id+"' /></li>");
    $(list).append(item);
    return false;
};

admin_medical_disease.remove_related = function(id, list_id) {
    var confirm = window.confirm('Bạn muốn đưa bài viết này ra khỏi danh sách liên quan');
    if (!confirm) {
        return false;
    }
    if (id == null || id == undefined) {
        return false;
    }
    var list = $("#" + list_id);
    var selected_item = $("#li_disease_relate" + id);
    if ($(selected_item).size() > 0) {
        var btn_related = $("#btn_disease_relate" + id);
        if ($(btn_related).size() > 0) {
            $(btn_related).show();
        }
        $(selected_item).remove();
    }

    var index = 0;
    $('#disease_related li').each(function(){
        index++;
    });

    if(index == 0){
        $("#disease_related").hide();
    }

    return false;
};

admin_medical_disease.filter_symptoms_related = function(result_id) {
    var list_id = "symptoms_related";

    var data = $("form").serialize();
    data += "&ajax_action=true";
    $.ajax({
        type: "POST",
        url: BASE_URL + "admin/medical/symptoms/symptoms_relate",
        data: data
    }).done(function(output) {
            $("#" + result_id).html(output);

            var list = $("#" + list_id);
            if ($(list).size() == 0) {
                return false;
            }
            $(list).find("li").each(function() {
                var id = $(this).attr("id").toString().replace("related_id_", "");
                var btn_related = $("#btn_related_" + id);
                if ($(btn_related).size() > 0) {
                    $(btn_related).hide();
                }
            });
        });
    return false;
};

admin_medical_disease.reset_filter_symptoms = function(result_id) {
    var list_id = "symptoms_related";

    var data = "ajax_action=true";
    $.ajax({
        type: "POST",
        url: BASE_URL + "admin/medical/symptoms/symptoms_relate",
        data: data
    }).done(function(output) {
            $("#" + result_id).html(output);

            var list = $("#" + list_id);
            if ($(list).size() == 0) {
                return false;
            }
            $(list).find("li").each(function() {
                var id = $(this).attr("id").toString().replace("li_symptoms_relate", "");
                var btn_related = $("#btn_symptoms_relate" + id);
                if ($(btn_related).size() > 0) {
                    $(btn_related).hide();
                }
            });
        });
    return false;
};

admin_medical_disease.add_symptoms_related = function(id, title,edit_url, list_id) {
    if (id == null || id == undefined) {
        return false;
    }
    if (title == null || title == undefined) {
        return false;
    }

    $("#" + list_id).show();

    var list = $("#" + list_id);
    if ($(list).size() == 0) {
        return false;
    }
    var selected_item = $("#li_symptoms_relate" + id);
    if ($(selected_item).size() > 0) {
        alert('Bệnh lý này đã được chọn');
        return false;
    }
    var btn_related = $("#btn_symptoms_relate" + id);
    if ($(btn_related).size() > 0) {
        $(btn_related).hide();
    }
    var item = $("<li id='li_symptoms_relate"+id+"'><span title='Xóa khỏi danh sách liên quan' class='icon icon_delete' onclick='admin_medical_disease.remove_symptoms("+id+")'></span><a target='_blank' href='"+edit_url+"'>"+title+"</a><input type='hidden' name='data[symptoms_ids][]' value='"+id+"' /></li>");
    $(list).append(item);
    return false;
};

admin_medical_disease.remove_symptoms = function(id) {
    var confirm = window.confirm('Bạn muốn xóa triệu chứng này khỏi danh sách liên quan');
    if (!confirm) {
        return false;
    }
    if (id == null || id == undefined) {
        return false;
    }
    var selected_item = $("#li_symptoms_relate" + id);
    if ($(selected_item).size() > 0) {
        var btn_related = $("#btn_symptoms_relate" + id);
        if ($(btn_related).size() > 0) {
            $(btn_related).show();
        }
        $(selected_item).remove();
    }

    var index = 0;
    $('#symptoms_related li').each(function(){
        index++;
    });

    if(index == 0){
        $("#symptoms_related").hide();
    }

    return false;
};

admin_medical_disease.add_video = function(){
    var index = 0;
    $(".area_video_disease").each(function(){
        index++;
    });
    index += 1;
    var html = '<div class="area_video_disease frm_field" id="area_video_'+ index +'">';
    html += '<p class="mb_10"><strong>Tiêu đề: </strong><input type="text" name="data[video]['+ index +'][title]" class="std_input ml_20" style="width:300px" /><a class="icon icon_delete" title="Xóa video" href="javascript:;void(0);" style="float: right;margin-top: 5px" onclick="admin_medical_disease.remove_video('+ index +')"></a></p>';
    html += '<p class="mb_10"><strong>Mã nhúng: </strong><input type="text" name="data[video]['+ index +'][embed]" class="std_input ml_6" /></p>';
    html += '<p class="mb_10"><strong>Mô tả ngắn: </strong><input type="text" name="data[video]['+ index +'][description]" class="std_input" /></p>';
    html += '</div>';

    if(index > 5){
        alert("Chỉ được up tối đa 5 video");
        return false;
    }
    $("#area_video_" + (index - 1)).after(html);
};

var __upload_index = 1;
admin_medical_disease.uploadMulti = function (button) {
    var uploader = new plupload.Uploader({
        runtimes: 'gears,html5,flash,silverlight,browserplus',
        browse_button: button,
        container: 'container-upload',
        max_file_size: '2mb',
        url: BASE_URL + "admin/medical/disease/upload_multi",
        flash_swf_url: '/plupload/js/plupload.flash.swf',
        silverlight_xap_url: '/plupload/js/plupload.silverlight.xap',
        filters: [
            {title: "Image files", extensions: "jpg,gif,png,jpeg"}
        ]
    });

    uploader.bind('Init', function (up, params) {
    });
    uploader.init();
    uploader.bind('FilesAdded', function (up, files) {

        $.each(files, function (i, file) {
            __upload_index++;
            var html = jQuery('.container_data_image_upload').html();
            var str = html.replace(/INDEX/gi, file.id);

            $(".add_other_image").append(str);
        });

        uploader.start();
        up.refresh(); // Reposition Flash/Silverlight
    });

    uploader.bind('UploadProgress', function (up, file) {
        $('#process_upload_' + file.id).show();
    });

    uploader.bind('Error', function (up, err) {
        $('#filelist').append("<div>Error: " + err.code +
            ", Message: " + err.message +
            (err.file ? ", File: " + err.file.name : "") +
            "</div>"
        );

        up.refresh(); // Reposition Flash/Silverlight
    });
    uploader.bind('FileUploaded', function (up, file, info) {

        var json = jQuery.evalJSON(info["response"]);

        if(json.mess != undefined && json.mess == 'error'){
            jQuery('#area_image_disease_' + file.id).remove();
            if (uploader.total.uploaded == uploader.files.length) {
                //đây là nơi check khi upload hết tất cả file
            }
            console.log(json.error);
        }else{
            jQuery('img#img_view_' + file.id).attr('src', json.image_url);

            jQuery('#image_val_' + file.id).attr('value', json.image);

            jQuery('#process_upload_' + file.id).hide();

            if (uploader.total.uploaded == uploader.files.length) {
                jQuery('#process-all').hide();
                //jQuery('.check_upload_sub_item').val(0);
                //eDialog.position = "fixed";
                //eDialog.alert('Upload danh sách hình ảnh sản phẩm thành công!');
                //alert('Upload danh sách hình ảnh sản phẩm thành công!');
            }
        }
    });
};

admin_medical_disease.remove_image_other = function(index){
    var msg = confirm("Bạn có muốn xóa ảnh này không");
    if(!msg){
        return false;
    }

    jQuery("#area_image_disease_" + index).remove();
};

admin_medical_disease.remove_video = function(index){
    var msg = confirm("Bạn có muốn xóa video này không");
    if(!msg){
        return false;
    }

    jQuery("#area_video_" + index).remove();
};